﻿using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Serilog;
using SheepStorage.API.Application.Command.SheepChangeDormCommand;
using SheepStorage.API.Baseclass;
using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Domain.Enum;
using SheepStorage.Service.Interface;

namespace SheepStorage.API.Controllers
{

	/// <summary>
	/// 羊只转舍模块
	/// </summary>
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class ChangeDormitoryController : ControllerBase
	{
		private readonly IChangeDormitoryService service;

		private readonly IMediator mediator;

		private readonly IMapper mapper;


		public ChangeDormitoryController(IChangeDormitoryService service, IMediator mediator, IMapper mapper)
		{
			this.service = service;
			this.mediator = mediator;
			this.mapper = mapper;
		}








		/// <summary>
		/// 显示羊只转舍列表 
		/// </summary>
		/// <returns></returns>

		[HttpPost]
		public async Task<Results<PageOutput<SheepChangeDormitoryOutput>>> GetChangeDormitories(ChangeDormShowCommand command)
		{

			return await mediator.Send(command);


		}
		/// <summary>
		/// 羊只转舍添加
		/// </summary>
		/// <returns></returns>

		[HttpPost]
		public async Task<Results<int>> AddChangeDormitory(ChangeDormAddCommand command)
		{

			return await mediator.Send(command);


		}



		/// <summary>
		/// 羊只转舍删除
		/// </summary>
		/// <param name="ChangeId"></param>
		/// <returns></returns>
		[HttpPost]
	 
		public async Task<Results<int>> DeleteChangeDormitory(ChangeDormDeleteCommand command)
		{
			try
			{
				return await mediator.Send(command);
			}
			catch (Exception ex)
			{
				Log.Error("逻辑删除出错" + ex.Message);
				throw;
			}

		}


		/// <summary>
		/// 羊只转舍审核
		/// </summary>
		/// <param name="ChangeId"></param>
		/// <param name="Status"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<Results<int>> UpdateChangeDormitoryState(int ChangeId, int Status)
		{
			var res = await service.UpdateChangeDormitoryState(ChangeId, Status);

			return Results<int>.DataResult(res);
		}






		/// <summary>
		/// 羊只转舍详情-反填
		/// </summary>
		/// <param name="ChangeDormId"></param>
		/// <returns></returns>
		[HttpGet]
		public async Task<Results<SheepChangeDormitoryOutput>> GetChangeDormitoryById(int ChangeDormId)
		{
			var entity = await service.GetChangeDormitoryById(ChangeDormId);
			return Results<SheepChangeDormitoryOutput>.DataResult(mapper.Map<SheepChangeDormitoryOutput>(entity));

		}

		/// <summary>
		/// 羊只转舍修改 
		/// </summary>
		/// <param name="model"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<Results<int>> UpdateChangeDormitory(SheepChangeDormitory model)
		{
			var res = await service.UpdateChangeDormitory(model);

			return Results<int>.DataResult(res);
		}





		/// <summary>
		/// 羊只转舍类型枚举
		/// </summary>
		/// <returns></returns>
		[HttpGet]
        public IActionResult GetChangeDormitoryTypesEnum()
        {
            //定义个集合把枚举值存进来
            List<object> list = new List<object>();
            foreach (ChangeDormitoryTypes item in Enum.GetValues(typeof(ChangeDormitoryTypes)))
            {
                list.Add(new
                {
                    //类型信息
                    name = item.ToString(),
                    //id
                    value = item
                });
            }

            return Ok(list);
        }

    }


}
